﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Card;
using DevExpress.XtraGrid.Views.Card.ViewInfo;
using DevExpress.XtraGrid.Views.Layout;
using System.IO;

namespace Lamex.mKanban.UI
{
    public partial class frmKan_Accessory : frmBaseChild
    {
        public frmKan_Accessory()
        {
            InitializeComponent();
        }

        private void frmSCH_Accessory_Load(object sender, EventArgs e)
        {
            initialLayoutViewProperies();
            string CurUser = Lamex.Common.Loginer.CurrentUser.Account;
            switch (CurUser)
            {
                case "Rocky He":
                    label6.Visible = true;
                    break;
                default:
                    label6.Visible = false;
                    break;
            }
        }

        //初始化设置LayoutView
        private void initialLayoutViewProperies()
        {
            //layoutView1.OptionsView.ShowViewCaption = true;//显示视图标题
            //layoutView1.ViewCaption = "配件明细";//设置视图标题

            //layoutView1.OptionsView.ShowHeaderPanel = false;//隐藏顶部工具栏

            //设置显示方式，先横后竖，最大显示列数，垂直方向翻页（这几个属性配合使用）
            layoutView1.OptionsView.ViewMode = LayoutViewMode.MultiRow;
            //layoutView1.OptionsMultiRecordMode.MaxCardColumns = 2;
            layoutView1.OptionsMultiRecordMode.MultiRowScrollBarOrientation = ScrollBarOrientation.Vertical;

            //layoutView1.OptionsView.ShowCardCaption = false;//隐藏卡片标题，不隐藏，避免漏项
            layoutView1.OptionsMultiRecordMode.MaxCardColumns = -1;//不起作用，卡片改为横向顺序排列

            //隐藏字段名操作过程
            //Run Designer-Layout-Template Card-(选择操作列，右键选Hidde Text 或设置 TextVisible=False)
            //数据超宽换行
            //Run Designer-main-columns的某个gridcolumn下的columnEdit为新建为(repositoryitem)memoedit 

            //用于配件看板必须设成分割式，提醒用户避免漏项
            //因排版问题（无法达到想要的效果），改为加不隐藏卡片标题
            //layoutView1.OptionsView.CardArrangeRule = LayoutCardArrangeRule.AllowPartialCards;

            layoutView1.OptionsBehavior.Editable = false;//禁止编辑

            //控制gridControl中cardview中图片的固定大小显示，当原始图片大小不一时使用
            /*
             * 打开Run Designer，view选项中的外观中设置cardWidth（也就显示的cardView的宽度），
             * 再进入In-Place Editor Repository,
             * 选中respositoryItemPictureEdit1在行为中设置CustomHeight（也就是cardView的高），
             * 把SizeMode改为Stretch即可
             */           
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            string strSql1 = @"SELECT a.Part_No AS 物料号码,Part_Desc AS 中文描述,OWED_QTY AS 欠货数量,Sell_TS AS'完成日期'
                            FROM
                            (SELECT Part_No,Part_Desc,
                            CAST(SUM(OWED_QTY*Part_QTY/Order_QTY) AS decimal(18,2)) AS OWED_QTY
                            FROM View_OP_Follow_Process_Time
                            WHERE PROCESS_CODE = 'D54'
                            GROUP BY Part_No,Part_Desc) AS a
                            LEFT JOIN
                            (SELECT Part_No,MIN(Sell_TS) AS Sell_TS
                            FROM View_OP_Follow_Process_Time
                            --WHERE PROCESS_CODE = 'D54'
                            GROUP BY Part_No) AS b
                            ON a.Part_No=b.Part_No
                            ORDER BY Sell_TS";

            setData_GridView(gridControl1, gridView1, strSql1);

            //gridView1.Columns[0].BestFit();
            //gridView1.Columns[2].BestFit();
            //gridView1.Columns[3].BestFit();

            //增加底部汇总
            gridView1.Columns["物料号码"].SummaryItem.FieldName = "物料号码";
            gridView1.Columns["物料号码"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            gridView1.Columns["欠货数量"].SummaryItem.FieldName = "欠货数量";
            gridView1.Columns["欠货数量"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;

//            string strSql = @"SELECT WORKER_NO,WORKER_NAME,DEPARTMENT,PHOTO
//                        FROM HR_Workers_Info
//                        WHERE WORKER_NAME='杨中英'";
//            DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
//            gridControl3.DataSource = dt;

        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            int iRow = gridView1.FocusedRowHandle;
            string strCurMat = gridView1.GetRowCellDisplayText(iRow, "物料号码").ToString();

            //Part_No='4032900660'
            string strSql2 = @"SELECT Package_Mo AS MO单号,OWED_QTY AS 欠货数量,Product_TS AS 生产日期,Sell_TS AS 完成日期
                        FROM View_OP_Follow_Process_Time
                        WHERE PROCESS_CODE='D54' 
                        AND Part_No='" + strCurMat + @"'";

            setData_GridView(gridControl2, gridView2, strSql2);

            string strSql3 = @"SELECT Material_No AS 物料,Unit_QTY AS 数量,Material_Desc AS 描述,PHOTO AS 图片
                        FROM
                        (SELECT Material_No,Unit_QTY,Material_Desc
                        FROM View_KB_Accessory_BOM_List
                        WHERE Part_No='" + strCurMat + @"') AS a
                        LEFT JOIN
                        KB_Accessory_Photo_List AS b
                        ON a.Material_No=b.FITTING_NO";

            DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql3);
            foreach(DataRow dr in dt.Rows)
            {
                string path = @"\\tx-fs10\TX-FS1\LAMEX\MES\SysSource\Image\Accessory\\" + dr["物料"].ToString() + @"-ISO" + @".JPG";
                if (File.Exists(path))
                    dr["图片"] = SaveImage( @"\\tx-fs10\TX-FS1\LAMEX\MES\SysSource\Image\Accessory\\" + dr["物料"].ToString() + @"-ISO" + @".JPG");
            }
            if (dt.Rows.Count >= 1)
            {
                gridControl3.DataSource = dt;
            }
            //gridView1.Columns[0].BestFit();
            //gridView1.Columns[2].BestFit();
            //gridView1.Columns[3].BestFit();
        }
        public byte[] SaveImage(String path)
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
            BinaryReader br = new BinaryReader(fs);
            byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //将流读入到字节数组中
            return imgBytesIn;
        }
        //更改gridcontrol控件cardview的cardCaption标题

        private void cardView1_CustomDrawCardCaption(object sender, DevExpress.XtraGrid.Views.Card.CardCaptionCustomDrawEventArgs e)
        {
            CardView view = sender as DevExpress.XtraGrid.Views.Card.CardView;
            (e.CardInfo as CardInfo).CaptionInfo.CardCaption = view.GetRowCellDisplayText(e.RowHandle, view.Columns[0]);
        }

        private void setData_GridView(GridControl gc, GridView gv, string strSql)
        {
            DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);

            if (dt.Rows.Count > 0)
            {
                gc.DataSource = dt;
                gv.OptionsBehavior.Editable = false;
                gv.OptionsView.ColumnAutoWidth = true;
                //refreshPositionStaduss();
            }
        }

        private void 隐藏选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 3;
        }

        private void 显示选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 66;
        }
    }
}